package com._4pd.demo.formal;

import com._4pd.demo.formal.crd.AiOperator;
import com._4pd.demo.formal.watcher.AiOperatorWatcher2;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinition;
import io.fabric8.kubernetes.api.model.apiextensions.v1.CustomResourceDefinitionBuilder;
import io.fabric8.kubernetes.client.dsl.Resource;
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext;

import java.io.File;

public class Main {
    public static void main(String[] args) throws Exception {
        K8sClient k8sClient = new K8sClient();
        // 注册crd
//        CustomResourceDefinitionBuilder customResourceDefinitionBuilder = CustomResourceDefinitionContext.v1CRDFromCustomResourceType(AiOperator.class);
//        CustomResourceDefinition crd = CustomResourceDefinitionContext.v1CRDFromCustomResourceType(AiOperator.class).build();
//        Resource<CustomResourceDefinition> resource = k8sClient.getClient().apiextensions().v1().customResourceDefinitions().resource(crd);
//        resource.createOrReplace();

        CustomResourceDefinition crd = k8sClient.getClient().apiextensions().v1()
                .customResourceDefinitions()
                .load(new File("target/classes/META-INF/fabric8/aioperator.demo.ai.operator-v1.yml"))
                .get();
        Resource<CustomResourceDefinition> resource = k8sClient.getClient().apiextensions().v1().customResourceDefinitions().resource(crd);
        if (resource.isReady()) {
            System.out.println("crd is ready");
            resource.delete();
        }
        resource.createOrReplace();

        k8sClient.registerWatcher(AiOperator.class, new AiOperatorWatcher2(k8sClient));
        System.out.println("service is ready");
    }
}
